#ifndef SHADER_H
#define SHADER_H

class Shader
{
public:
	Shader () ;
	Shader (const char * vp_source, const char * fp_source) ;

	void init ()
	{
		int major;
		int minor;
		getGLVersion(& major, & minor);
//		std::cout<<major<<' '<<minor<<std::endl;

		getGLSLVerstion(&major, & minor);
//		std::cout<<major<<' '<<minor<<std::endl;

		glewInit();
		if (glewIsSupported("GL_VERSION_2_0")) ;
//			printf("Ready for OpenGL 2.0\n");
		else {
			throw("OpenGL 2.0 not supported\n");
		}
		
	}
	void load(const char * vp_source, const char * fp_source) ;
	void bind();
	void unbind (); 
	int get_uniform(const char * uniform_name) const;
	void set_uniform(const char * uniform_name, int i) const;
	void set_uniform(const char * uniform_name, float f) const;
	void set_uniform(const char * uniform_name, const float vector4[], unsigned size) const;

	void getGLVersion(int * major, int * minor)
	{
		const char * verstr = (const char *) glGetString (GL_VERSION) ;
		if ((verstr == NULL) || (sscanf(verstr, "%d.%d", major, minor) != 2))
		{
			* major = * minor = 0;
			fprintf(stderr, "Invalid GL_VERSION format!!! \n");
		}
	}
	void getGLSLVerstion(int * major, int * minor)
	{
		int gl_major, gl_minor;
		getGLVersion(& gl_major, &gl_minor);
		if (gl_major >= 2)
		{
			const char * verstr = (const char *) glGetString (GL_SHADING_LANGUAGE_VERSION);
			if ((verstr == NULL) || (sscanf(verstr, "%d.%d", major, minor) != 2))
			{	
				* major = * minor = 0;
				fprintf(stderr, "Invalid GL_VERSION format!!! \n");
			}
		}
	}
protected:
	
	int printOglError(char *file, int line);
	void printShaderInfoLog(GLuint obj);
	void printProgramInfoLog(GLuint obj);

	char * load_source(const char *fn);
	int textFileWrite(char *fn, char *s);

private:
	GLuint v,f,p;
} ;

#endif
